package com.zkt.subject.common.entity;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;
import java.util.List;

/**
 * @author 赵开泰
 * @program jc-club
 * @date 2025/2/25
 * @description 分页返回的实体
 **/

@Getter
public class PageResult<T> implements Serializable {
	
	/**
	 * 当前页码
	 */
	@Setter
	private Integer pageNo = 1;
	
	/**
	 * 每页数量
	 */
	@Setter
	private Integer pageSize = 15;
	
	/**
	 * 总条数
	 */
	private Long total = 0L;
	
	/**
	 * 总页数
	 */
	private Long totalPages = 0L;
	
	/**
	 * 结果数据
	 */
	@Setter
	private List<T> result = ListUtil.empty();
	
	/**
	 * 起始位置
	 */
	private Integer start = 1;
	
	/**
	 * 结束位置
	 */
	private Integer end = 0;
	
	public void setTotal(Long total) {
		this.total = total;
		if (this.pageSize > 0) {
			this.totalPages = (total / this.pageSize) + (total % this.pageSize == 0 ? 0 : 1);
		} else {
			this.totalPages = 0L;
		}
		this.start = (this.pageSize > 0 ? (this.pageNo - 1) * this.pageSize : 0) + 1;
		this.end = (this.start - 1 + this.pageSize * (this.pageNo > 0 ? 1 : 0));
	}
	
}